package com.srs.dao;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.srs.model.Group;
import com.srs.model.RGroupRole;
import com.srs.model.RGroupScope;
import com.srs.model.User;
import com.srs.util.HibernateUtil;

public class RGroupRoleDao {

	final static Logger logger = LoggerFactory.getLogger(RGroupRoleDao.class);

	private List<RGroupRole> rGroupRoleList;
	private RGroupRole rGroupRole;

	public void delete(Integer rGroupRoleId) {

		Session session = HibernateUtil.getSession();
		Transaction tx = null;
		try {
			tx = session.beginTransaction();
			rGroupRole = (RGroupRole) session.get(RGroupRole.class, rGroupRoleId);
			session.delete(rGroupRole);
			tx.commit();

		} catch (RuntimeException e) {
			if (tx != null)
				tx.rollback();
			throw e;
		} finally {
			session.close();
		}
	}

	public void deleteByGroupId(Integer groupId) {

		Session session = HibernateUtil.getSession();
		Transaction tx = null;
		try {
			tx = session.beginTransaction();
			Query q = session.createQuery("delete RGroupRole r where r.group.groupId=:groupId");
			q.setInteger("groupId", groupId);
			q.executeUpdate();			
			tx.commit();

		} catch (RuntimeException e) {
			if (tx != null)
				tx.rollback();
			throw e;
		} finally {
			session.close();
		}
	}
	
	@SuppressWarnings("unchecked")
	public List<RGroupRole> list() {
		Session session = HibernateUtil.getSession();
		try {
			session.beginTransaction();
			rGroupRoleList = session.createQuery("from RGroupRole").list();
			return rGroupRoleList;

		} catch (Exception e) {
			System.out.print("Error while fetching " + e);
			return null;
		} finally {
			session.close();
		}

	}

	@SuppressWarnings("unchecked")
	public List<RGroupRole> list(Group group) {
		Session session = HibernateUtil.getSession();
		try {
			session.beginTransaction();
			Query q = session.createQuery("from RGroupRole r where r.group.groupId=:groupId");
			q.setInteger("groupId", group.getGroupId());
			rGroupRoleList = q.list();
			if (rGroupRoleList==null){
				return new ArrayList<RGroupRole>();
			}
			return rGroupRoleList;

		} catch (Exception e) {
			System.out.print("Error while fetching " + e);
			return new ArrayList<RGroupRole>();
		} finally {
			session.close();
		}

	}	

	@SuppressWarnings("unchecked")
	public boolean roleIsUsed(Integer roleId) {
		Session session = HibernateUtil.getSession();
		try {
			session.beginTransaction();
			Query q = session.createQuery("from RGroupRole r where r.roleId=:roleId");
			q.setInteger("roleId", roleId);
			rGroupRoleList = q.list();
			if (rGroupRoleList.size()==0){
				return false;
			}
			return true;

		} catch (Exception e) {
			System.out.print("Error while fetching " + e);
			return false;
		} finally {
			session.close();
		}

	}		
	
	
	public RGroupRole get(Integer rGroupRoleId) {
		Session session = HibernateUtil.getSession();
		try {
			session.beginTransaction();
			// change query for get proper data
			Query q = session.createQuery("from RGroupRole r where r.rGroupRoleId=:rGroupRoleId");
			q.setInteger("rGroupRoleId", rGroupRoleId);
			return (RGroupRole) q.uniqueResult();
		} finally {
			session.close();
		}

	}

	public void insert(RGroupRole rGroupRole) {
		Session session = HibernateUtil.getSession();
		Transaction tx = null;
		try {
			tx = session.beginTransaction();
			session.save(rGroupRole);
			tx.commit();
		} catch (RuntimeException e) {
			if (tx != null)
				tx.rollback();
			throw e;
		} finally {
			session.close();
		}

	}

	public void update(RGroupRole rGroupRole) {
		Session session = HibernateUtil.getSession();
		Transaction tx = null;
		try {
			tx = session.beginTransaction();
			session.update(rGroupRole);
			tx.commit();
		} catch (RuntimeException e) {
			if (tx != null)
				tx.rollback();
			throw e;
		} finally {
			session.close();
		}

	}

	public static void main(String[] args) {
		// Init sessionFactory
		SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
		HibernateUtil.setSessionFactory(sessionFactory);
		
		// Get list params
		RGroupRoleDao groupRoleDao = new RGroupRoleDao();
		List<RGroupRole> groupRoles = groupRoleDao.list();
		GroupDao groupDao = new GroupDao();
		for (RGroupRole rGroupRole : groupRoles) {
			Group group = groupDao.get(rGroupRole.getGroup().getGroupId());
			if (group.getGroupId()==null){
				System.out.println(rGroupRole.getGroup().getGroupId());
				groupRoleDao.deleteByGroupId(rGroupRole.getGroup().getGroupId());
			}
		}
		
		RGroupScopeDao groupScopeDao = new RGroupScopeDao();
		List<RGroupScope> groupScopes = groupScopeDao.list();
		GroupDao scopeDao = new GroupDao();
		for (RGroupScope rGroupScope : groupScopes) {
			Group group = scopeDao.get(rGroupScope.getGroup().getGroupId());
			if (group.getGroupId()==null){
				System.out.println(rGroupScope.getGroup().getGroupId());
				groupScopeDao.deleteByGroupId(rGroupScope.getGroup().getGroupId());
			}
		}		
	}	
	
}
